package cn.edu.zjut.base;
import java.awt.Font;
import java.awt.FontFormatException;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @description:
 * @author:ZhouDan
 * @date: 2020/8/11
 * @version: 1.0
 */

public class CyFont {
    private Font definedFont = null;

    public Font getDefinedFont(int ft,float fs) {
        String fontUrl;
        switch (ft) {
            case 1:
                fontUrl="/FZXRJW GB1 0.ttf";
                break;
            default:
                String fonturllocal="/usr/share/fonts/wqy-zenhei/wqy-zenhei.ttc";
                if(!new File(fonturllocal).exists()){
                    fontUrl="/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc";
                }else{
                    fontUrl=fonturllocal;
                }
                break;
        }
        if (definedFont == null) {
            InputStream is = null;
            BufferedInputStream bis = null;
            try {
                is =new FileInputStream(new File(fontUrl));
                bis = new BufferedInputStream(is);
                definedFont = Font.createFont(Font.TRUETYPE_FONT, is);
                //设置字体大小，float型
                definedFont = definedFont.deriveFont(fs);
            } catch (FontFormatException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (null != bis) {
                        bis.close();
                    }
                    if (null != is) {
                        is.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return definedFont;
    }


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        CyFont cf=new CyFont();
        cf.getDefinedFont(1, (float) 120.0);

    }

}
